package cz.cuni.mff.ufal.volk.boot;

import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import cz.cuni.mff.ufal.volk.InitializationException;
import cz.cuni.mff.ufal.volk.utils.Utils;

public class DefaultBootCodeLoader implements BootCodeLoader {

	public DefaultBootCodeLoader() {
		InputStream in = DefaultBootCodeLoader.class
		    .getResourceAsStream("/volk/resources/bootcode.bin");
		try {
			try {
				bootCodeBytes = Utils.readAllBytes(in, 10000);
			} finally {
				in.close();
			}
		} catch (IOException e) {
			throw new InitializationException("An IOException occurred during DefaultBootCodeLoader initialization", e);
		}
	}

	private final byte[] bootCodeBytes;

	@Override
	public BootCode loadBootCode(String version, String serial) {
		return new BootCode() {

			@Override
			public long size() {
				return bootCodeBytes.length;
			}

			@Override
			public InputStream getInputStream() throws FileNotFoundException {
				return new ByteArrayInputStream(bootCodeBytes);
			}
		};
	}

}
